using System;
using System.Text;
using System.IO;
using System.Security.Cryptography;
using Microsoft.Win32;

namespace keygen
{
    public class Register
    {
        // Fields
        private SymmetricAlgorithm EncryptionService;
        private string IV;
        private string Key;
        private static RegistryKey rootkey;

        // Methods
        public Register()
        {
            rootkey = Registry.CurrentUser;
            this.EncryptionService = new RijndaelManaged();
            this.Key = "zychange#hnzzchkjGuz(%&hj7x89H$yuBasdfI0456AGFF#$%#$%#$$HFtmamamamaT5&fvHUFCy76*h%(HilJ$lhj!y6&(*jkP87jH7shell&%$#";
            this.IV = "zychange#hnzzchkjE4ghj*Ghg7!rNIfb&95GUY8QETSFHXDJ$^%#$$&%&@@dfdsdU6GfsadfsdfdikdikghUb#er57HBh(u%g6HJ($jhWk7&!hg4ui%$hjkshell&%$#";
        }

        public Register(string RootKey)
        {
            switch (RootKey.ToUpper())
            {
                case "CLASSES_ROOT":
                    rootkey = Registry.ClassesRoot;
                    break;
                case "CURRENT_USER":
                    rootkey = Registry.CurrentUser;
                    break;
                case "LOCAL_MACHINE":
                    rootkey = Registry.LocalMachine;
                    break;
                case "USERS":
                    rootkey = Registry.Users;
                    break;
                case "CURRENT_CONFIG":
                    rootkey = Registry.CurrentConfig;
                    break;
                case "DYN_DATA":
                    rootkey = Registry.PerformanceData;
                    break;
                case "PERFORMANCE_DATA":
                    rootkey = Registry.PerformanceData;
                    break;
                default:
                    rootkey = Registry.CurrentUser;
                    break;
            }
            this.EncryptionService = new RijndaelManaged();
            this.Key = "zychange#hnzzchkjGuz(%&hj7x89H$yuBasdfI0456AGFF#$%#$%#$$HFtmamamamaT5&fvHUFCy76*h%(HilJ$lhj!y6&(*jkP87jH7shell&%$#";
            this.IV = "zychange#hnzzchkjE4ghj*Ghg7!rNIfb&95GUY8QETSFHXDJ$^%#$$&%&@@dfdsdU6GfsadfsdfdikdikghUb#er57HBh(u%g6HJ($jhWk7&!hg4ui%$hjkshell&%$#";
        }

        public Register(string key, string iv)
        {
            rootkey = Registry.CurrentUser;
            this.EncryptionService = new RijndaelManaged();
            this.Key = key;
            this.IV = iv;
        }

        public Register(string RootKey, string key, string iv)
        {
            switch (RootKey.ToUpper())
            {
                case "CLASSES_ROOT":
                    rootkey = Registry.ClassesRoot;
                    break;
                case "CURRENT_USER":
                    rootkey = Registry.CurrentUser;
                    break;
                case "LOCAL_MACHINE":
                    rootkey = Registry.LocalMachine;
                    break;
                case "USERS":
                    rootkey = Registry.Users;
                    break;
                case "CURRENT_CONFIG":
                    rootkey = Registry.CurrentConfig;
                    break;
                case "DYN_DATA":
                    rootkey = Registry.PerformanceData;
                    break;
                case "PERFORMANCE_DATA":
                    rootkey = Registry.PerformanceData;
                    break;
                default:
                    rootkey = Registry.CurrentUser;
                    break;
            }
            rootkey = Registry.CurrentUser;
            this.EncryptionService = new RijndaelManaged();
            this.Key = key;
            this.IV = iv;
        }

        public RegistryKey CreateRegKey(string keypath)
        {
            try
            {
                return rootkey.CreateSubKey(keypath);
            }
            catch
            {
                return null;
            }
        }

        public string Decrypto(string Source)
        {
            try
            {
                byte[] buffer = Convert.FromBase64String(Source);
                MemoryStream stream = new MemoryStream(buffer, 0, buffer.Length);
                this.EncryptionService.Key = this.GetKey();
                this.EncryptionService.IV = this.GetIV();
                ICryptoTransform transform = this.EncryptionService.CreateDecryptor();
                CryptoStream stream2 = new CryptoStream(stream, transform, CryptoStreamMode.Read);
                StreamReader reader = new StreamReader(stream2);
                return reader.ReadToEnd();
            }
            catch
            {
                return string.Empty;
            }
        }

        public bool DelRegKeyVal(string keypath, string keyname)
        {
            try
            {
                rootkey.OpenSubKey(keypath, true).DeleteValue(keyname);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool DelRegSubKey(string keypath)
        {
            try
            {
                rootkey.DeleteSubKey(keypath);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool DelRegSubKeyTree(string keypath)
        {
            try
            {
                rootkey.DeleteSubKeyTree(keypath);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public string Encrypto(string Source)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(Source);
            MemoryStream stream = new MemoryStream();
            this.EncryptionService.Key = this.GetKey();
            this.EncryptionService.IV = this.GetIV();
            ICryptoTransform transform = this.EncryptionService.CreateEncryptor();
            CryptoStream stream2 = new CryptoStream(stream, transform, CryptoStreamMode.Write);
            stream2.Write(bytes, 0, bytes.Length);
            stream2.FlushFinalBlock();
            stream.Close();
            return Convert.ToBase64String(stream.ToArray());
        }

        public static string GetDecrypt(string source)
        {
            Register register = new Register();
            return register.Decrypto(source);
        }

        public static string GetEncrypt(string source)
        {
            Register register = new Register();
            return register.Encrypto(source);
        }

        private byte[] GetIV()
        {
            string iV = this.IV;
            this.EncryptionService.GenerateIV();
            int length = this.EncryptionService.IV.Length;
            if (iV.Length > length)
            {
                iV = iV.Substring(0, length);
            }
            else if (iV.Length < length)
            {
                iV = iV.PadRight(length, ' ');
            }
            return Encoding.ASCII.GetBytes(iV);
        }

        private byte[] GetKey()
        {
            string key = this.Key;
            this.EncryptionService.GenerateKey();
            int length = this.EncryptionService.Key.Length;
            if (key.Length > length)
            {
                key = key.Substring(0, length);
            }
            else if (key.Length < length)
            {
                key = key.PadRight(length, ' ');
            }
            return Encoding.ASCII.GetBytes(key);
        }

        public string GetRegVal(string keypath, string keyname, string def)
        {
            try
            {
                return rootkey.OpenSubKey(keypath).GetValue(keyname, def).ToString();
            }
            catch
            {
                return def;
            }
        }

        public bool IStrueMAC(string LocalMac, string CodeMac)
        {
            return (LocalMac == CodeMac);
        }

        public bool SetRegVal(string keypath, string keyname, string keyval)
        {
            try
            {
                rootkey.OpenSubKey(keypath, true).SetValue(keyname, keyval);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public string SKeys(string JMac, string JZDate, string Pnum, string RegTo)
        {
            return this.Encrypto(JMac + "-" + JZDate + "-" + Pnum + "-" + RegTo);
        }

        public string SPKeys(string keys)
        {
            return this.Decrypto(keys);
        }

        public string SplitCode(string Code)
        {
            return this.Decrypto(Code);
        }
    }
}
